package cn.edu.nju.software.action.comment;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import cn.edu.nju.software.bean.show.BookShow;
import cn.edu.nju.software.bean.show.CommentSearchListItem;
import cn.edu.nju.software.bean.show.CommentShow;
import cn.edu.nju.software.bean.show.UserShow;
import cn.edu.nju.software.bean.storage.Comment;
import cn.edu.nju.software.service.intf.CommentService;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class GetCommentsAction extends ActionSupport {

	private String user;
	private String title;
	private Date beginDate;
	private Date endDate;
	private Map<Integer, CommentShow> commentShows;
	private CommentService commentService;
	
	public void setUser(String user) {
		this.user = user;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

	public Map<Integer, CommentShow> getCommentShows() {
		return commentShows;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
	
	@Override
	public String execute() throws Exception {
		if (user==null && title==null && beginDate==null && endDate==null) {
			return INPUT;
		}
		List<CommentSearchListItem> commentItems = commentService.searchComments(user, title, beginDate, endDate);
		commentShows = new LinkedHashMap<Integer, CommentShow>(commentItems.size());
		for (CommentSearchListItem commentItem : commentItems) {
			UserShow user = new UserShow(commentItem.getUserName());
			BookShow book = new BookShow(commentItem.getBookName());
			Comment comment = commentItem.getComment();
			commentShows.put(comment.getId(), new CommentShow(comment, user, book));
		}
		commentItems = null;
		user = null;
		title = null;
		beginDate = null;
		endDate = null;
		return SUCCESS;
	}

}
